package com.example.mydy;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.mydy.adapter.ActivityRankingAdapter;
import com.example.mydy.model.ActivityData;
import com.example.mydy.utils.ActivityManager;
import com.example.mydy.utils.MockDataManager;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class RankingsActivity extends AppCompatActivity {

    private static final String TAG = "RankingsActivity";

    private TabLayout tabLayout;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView emptyView;

    private ActivityRankingAdapter adapter;
    private ActivityManager activityManager;
    private String currentUsername = "user1"; // 默认用户名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rankings);

        // 初始化管理器
        activityManager = ActivityManager.getInstance(this);

        // 初始化视图
        initViews();

        // 设置选项卡和适配器
        setupTabLayout();
        setupRecyclerView();
        setupSwipeRefresh();

        // 加载排行榜数据
        loadRankingData("monthly");
    }

    private void initViews() {
        tabLayout = findViewById(R.id.tab_layout);
        recyclerView = findViewById(R.id.recycler_view);
        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        emptyView = findViewById(R.id.empty_view);

        // 设置返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
    }

    private void setupTabLayout() {
        tabLayout.addTab(tabLayout.newTab().setText("月度排行"));
        tabLayout.addTab(tabLayout.newTab().setText("周度排行"));
        tabLayout.addTab(tabLayout.newTab().setText("连续登录"));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String type;
                switch (tab.getPosition()) {
                    case 0:
                        type = "monthly";
                        break;
                    case 1:
                        type = "weekly";
                        break;
                    case 2:
                        type = "login";
                        break;
                    default:
                        type = "monthly";
                        break;
                }
                loadRankingData(type);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 重新加载数据
                onTabSelected(tab);
            }
        });
    }

    private void setupRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ActivityRankingAdapter(this, new ArrayList<>(), currentUsername);
        recyclerView.setAdapter(adapter);
    }

    private void setupSwipeRefresh() {
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(() -> {
            int selectedTabPosition = tabLayout.getSelectedTabPosition();
            String type;
            switch (selectedTabPosition) {
                case 0:
                    type = "monthly";
                    break;
                case 1:
                    type = "weekly";
                    break;
                case 2:
                    type = "login";
                    break;
                default:
                    type = "monthly";
                    break;
            }
            loadRankingData(type);
        });
    }

    private void loadRankingData(String type) {
        swipeRefreshLayout.setRefreshing(true);

        List<ActivityData> rankingList = new ArrayList<>();
        // 假定这些方法存在或已经实现
        try {
            switch (type) {
                case "monthly":
                    rankingList = activityManager.getMonthlyRankings(20);
                    break;
                case "weekly":
                    rankingList = activityManager.getWeeklyRankings(20);
                    break;
                case "login":
                    rankingList = activityManager.getConsecutiveLoginRankings(20);
                    break;
                default:
                    rankingList = activityManager.getMonthlyRankings(20);
                    break;
            }
        } catch (Exception e) {
            // 如果方法不存在，创建一些测试数据
            for (int i = 0; i < 10; i++) {
                ActivityData data = new ActivityData();
                data.setUsername("user" + i);
                data.setNickname("用户" + i);
                data.setMonthlyActivity(1000 - i * 100);
                rankingList.add(data);
            }
        }

        adapter.updateData(rankingList);
        if (rankingList.isEmpty()) {
            emptyView.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }

        swipeRefreshLayout.setRefreshing(false);
    }
} 